﻿#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtCore>
#include "qtmbackendtask.h"
#include <Ice/Ice.h>
#include <thread>
#include "RobotDef.h"

QTmBackendTask* g_pBackendTask = NULL;

class RobotScreenExI : public Robot::ScreenEx{
public:
    RobotScreenExI(){}

    // ScreenEx interface
public:
    void ShowRepeatDir(Ice::Int nDelay, const std::string &strDir, const Ice::Current &){
        if( !g_pBackendTask ){
            return;
        }
        QString strDirPath = QString::fromUtf8(strDir.c_str());
        g_pBackendTask->proc_repeat_dir(nDelay, strDirPath);
    }

    void ShowPic(Ice::Int nDelay, const std::string &strPath, const Ice::Current &){
        if( !g_pBackendTask ){
            return;
        }
        qDebug()<<"ShowPic----------------";
        QString strPicPath = QString::fromUtf8(strPath.c_str());
        g_pBackendTask->qml_ui_show_pic(nDelay, strPicPath);
    }

    void ShowVideo(Ice::Int nDelay, const std::string &strPath, int nVol, const Ice::Current &){
        if( !g_pBackendTask ){
            return;
        }
        QString strVideoPath = QString::fromUtf8(strPath.c_str());
        g_pBackendTask->qml_ui_show_video(nDelay, strVideoPath, nVol);
    }

    void Clean(const Ice::Current &){
        if( !g_pBackendTask ){
            return;
        }
        g_pBackendTask->qml_ui_show_clean();
    }

    void ShowVip(const std::string &strName, const std::string &strImgPath, const Ice::Current &){
        if( !g_pBackendTask ){
            return;
        }
        QString strVipName = QString::fromUtf8(strName.c_str());
        QString strVipPic = QString::fromUtf8(strImgPath.c_str());
        g_pBackendTask->qml_ui_show_vip(strVipPic, strVipName);
    }

    void ShowBarrage(Ice::Int nDelay, const std::string &strTxt, const Ice::Current &){
        if( !g_pBackendTask ){
            return;
        }
        QString strBarrageTxt = QString::fromUtf8(strTxt.c_str());
        g_pBackendTask->qml_ui_show_barrage(strBarrageTxt, nDelay);
    }
    void ShowVideoVip(Ice::Int nType, const::std::string &strName, const Ice::Current &){
        if( !g_pBackendTask ){
            return;
        }
        QString strVipName = QString::fromUtf8(strName.c_str());
        g_pBackendTask->qml_ui_video_vip(nType,strVipName);
    }

};


class Server : public Ice::Application
{
public:
    Server(QCoreApplication* p){
        m_pApp = p;
    }
    virtual int run(int argc, char* argv[]){
        // Ice object
        Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("Robot");

        // ScreenEx
        RobotScreenExI* pScreenExI = new RobotScreenExI();
        adapter->add(pScreenExI, communicator()->stringToIdentity("ScreenEx"));

        adapter->activate();

        communicator()->waitForShutdown();

        QCoreApplication::processEvents();
        m_pApp->exit();
        return 0;
    }
private:
    QCoreApplication* m_pApp;

};

void t_run(QCoreApplication* p){
    Server app(p);
    app.main(__argc, __argv);
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
//    QSharedMemory oneInstance("TamiServerScreenEx");
//    if( !oneInstance.create(1) ){
//        qDebug()<<"TamiServerScreenEx already exist!";
//        return 0;
//    }
    // Init
    QString strRoot = QCoreApplication::applicationDirPath();
    QDir::setCurrent(strRoot);
    // ui
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("UI_ScreenEx/main.qml")));
    QList<QObject*> pListObj = engine.rootObjects();
    if( pListObj.size()<1 ){
        qDebug()<<"Get UI_ScreenEx Backend Objs Failed!";
        return 0;
    }
    qDebug()<<pListObj;
    // init back task
    g_pBackendTask = new QTmBackendTask();
    qDebug()<<"Init UI_ScreenEx:"<<g_pBackendTask->init(pListObj[0]);

    std::thread t1(t_run, &a);
    t1.detach();
    return a.exec();
}
